import os
import threading

from celery.utils.log import get_task_logger
from flask import has_app_context

from tools import celery_app

# flake8: noqa

logger = get_task_logger(__name__)


@celery_app.task(bind=True, name="task_debug")
def task_debug(self, *args, **kwargs):
    """debug 任务 校验当前环境celery能否得到转储的request context和flask context

    from app.api.v1.participant.tasks import task_debug
    task_debug.delay()
    """
    logger.info(self.request.headers)
    logger.info(f"task id: {self.request.id}, args: {args!r}, kwargs: {kwargs!r}")
    logger.info(
        f"curl http://127.0.0.1:5000/ping/{self.request.id}/"
        f"{os.getpid()}/{threading.get_ident()}/{has_app_context()}"
    )

    return True
